//
//  ExpressInfoViewController.m
//  PrintPort
//
//  Created by hy002 on 2018/1/2.
//  Copyright © 2018年 HUAYU. All rights reserved.
//

#import "ExpressInfoViewController.h"
#import "ExpressModel.h"

static NSString *cellPath = @"ExpressCell";
@interface ExpressInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)NSArray *infoAry;
@property (nonatomic, assign)BOOL isQianShou;

@end

@implementation ExpressInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
    [self requestOrderExpressInfo];
}

- (void)creatUI
{
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"物流详情";
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableview];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0)
    {
        UIImageView *imgView = [ZCControl createImageViewWithFrame:CGRectMake(15*displaywidth, 10*displaywidth, 60*displaywidth, 60*displaywidth) ImageName:@""];
        if (_orderModel.productImgUrl.length > 0)
        {
            [imgView sd_setImageWithURL:[NSURL URLWithString:_orderModel.productImgUrl]];
        }
        imgView.contentMode = UIViewContentModeScaleAspectFill;
        imgView.layer.masksToBounds = YES;
        [cell.contentView addSubview:imgView];
        UILabel *proNameLab = [ZCControl createLabelWithFrame:CGRectMake(85*displaywidth, 10*displaywidth, SCREEN_WIDTH-100*displaywidth, 30*displaywidth) Font:14.0 Text:[NSString stringWithFormat:@"%@",_orderModel.productName] Color:RGB(77, 77, 77)];
        proNameLab.lineBreakMode = NSLineBreakByTruncatingTail;
        [cell.contentView addSubview:proNameLab];
        UILabel *expreNameLab = [ZCControl createLabelWithFrame:CGRectMake(85*displaywidth, 40*displaywidth, SCREEN_WIDTH-100*displaywidth, 30*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@"%@:%@",_orderModel.expressCompanyName,_orderModel.expressNo] Color:RGB(177, 177, 177)];
        expreNameLab.lineBreakMode = NSLineBreakByTruncatingTail;
        [cell.contentView addSubview:expreNameLab];
        UIView *lineAview = [[UIView alloc] initWithFrame:CGRectMake(0, 79*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
        lineAview.backgroundColor = LinesColor;
        [cell.contentView addSubview:lineAview];
        
    }
    else
    {
        UIImageView *addImgView = [ZCControl createImageViewWithFrame:CGRectMake(50*displaywidth, 20*displaywidth, 20*displaywidth, 20*displaywidth) ImageName:@"dizhi"];
        addImgView.contentMode = UIViewContentModeScaleAspectFit;
        [cell.contentView addSubview:addImgView];
        UILabel *addressLab = [ZCControl createLabelWithFrame:CGRectMake(80*displaywidth, 10*displaywidth, SCREEN_WIDTH-100*displaywidth, 45*displaywidth) Font:14.0 Text:[NSString stringWithFormat:@"[收货地址]%@%@%@%@",_orderModel.provinceText,_orderModel.cityText,_orderModel.districtText,_orderModel.detailAddress] Color:RGB(239, 146, 54)];
        addressLab.lineBreakMode = NSLineBreakByTruncatingTail;
        [cell.contentView addSubview:addressLab];
        CGFloat H = 60*displaywidth;
        if (_infoAry.count > 0)
        {
            NSInteger K = _infoAry.count-1;
            for (int i = 0; i < _infoAry.count; i++)
            {
                ExpressModel *model = _infoAry[K-i];
                UILabel *tostLab = [ZCControl createLabelWithFrame:CGRectMake(80*displaywidth, H+10*displaywidth, SCREEN_WIDTH-100*displaywidth, 45*displaywidth) Font:11.0 Text:model.AcceptStation Color:RGB(128, 128, 128)];
                [cell.contentView addSubview:tostLab];
                NSString *imgStr = @"paisong";
                if (_isQianShou && i == 0)
                {
                    imgStr = @"qianshou";
                }
                UIImageView *statuImgView = [ZCControl createImageViewWithFrame:CGRectMake(50*displaywidth, H+20*displaywidth, 20*displaywidth, 20*displaywidth) ImageName:imgStr];
                statuImgView.contentMode = UIViewContentModeScaleAspectFit;
                [cell.contentView addSubview:statuImgView];
                if (i > 0)
                {
                    UIView *lineLview = [[UIView alloc] initWithFrame:CGRectMake(59.5*displaywidth, H-20*displaywidth, 1*displaywidth, 40*displaywidth)];
                    lineLview.backgroundColor = RGB(179, 179, 179);
                    [cell.contentView addSubview:lineLview];
                }
                H+=60*displaywidth;
            }
        }
        else
        {
            UILabel *tostLab = [ZCControl createLabelWithFrame:CGRectMake(80*displaywidth, H+10*displaywidth, SCREEN_WIDTH-100*displaywidth, 20*displaywidth) Font:11.0 Text:@"暂无物流信息" Color:RGB(77, 77, 77)];
            [cell.contentView addSubview:tostLab];
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 80*displaywidth;
    }
    else
    {
        if (_infoAry.count > 0)
        {
            return 60*(_infoAry.count + 1)+40*displaywidth;
        }
        return SCREEN_HEIGHT-STA_NAV_HEIGHT-80*displaywidth;
    }
}


//查看物流
- (void)requestOrderExpressInfo
{
    WEAKSELF;
    NSDictionary *dic = @{@"orderMasterId":s_str(_orderModel.ID)};
    [HYBaseHandler requestOrderExpressInfoParmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            NSDictionary *dataDic = [weakSelf dictionaryWithJsonString:obj[@"data"]];
            NSString *typeStr = dataDic[@"State"];
            if ([typeStr integerValue] == 3)
            {
                weakSelf.isQianShou = YES;
            }
            weakSelf.infoAry = [ExpressModel mj_objectArrayWithKeyValuesArray:dataDic[@"Traces"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableview reloadData];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    
    if (jsonString == nil) {
        
        return nil;
        
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSError *err;
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                         
                                                        options:NSJSONReadingMutableContainers
                         
                                                          error:&err];
    
    if(err) {
        
        NSLog(@"json解析失败：%@",err);
        
        return nil;
        
    }
    
    return dic;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
